import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token'
// 导入页面
// import Login from '@/views/login/Login.vue'
// import Layout from '@/views/layout/Index'
// import Chart from '../views/layout/chart/Index'
// import User from '../views/layout/user/Index'
// import Question from '../views/layout/question/Index'
// import Enterprice from '../views/layout/enterprise/Index'
// import Subject from '../views/layout/subject/Index'
// import Welcome from '../views/layout/welcome/Index.vue'

// 实现路由懒加载
const Login = () => import('@/views/login/Login')
const Layout = () => import('@/views/layout/Index')
const Chart = () => import('@/views/layout/chart/Index')
const User = () => import('@/views/layout/user/Index')
const Enterprise = () => import('@/views/layout/enterprise/Index')
const Question = () => import('@/views/layout/question/Index')
const Subject = () => import('@/views/layout/subject/Index')
const Welcome = () => import('@/views/layout/welcome/Index')

Vue.use(VueRouter)
const routes = [
  {
    // 重定向c
    path: '/',
    redirect: '/login'
  },
  {
    // 登录路由
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录' }
  },
  {
    // 首页
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: 'welcome',
        component: Welcome,
        meta: {
          title: '欢迎页面',
          icon: 'el-icon-info',
          path: '/layout/welcome',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'chart',
        component: Chart,
        // 路由元信息
        meta: {
          title: '数据预览',
          icon: 'el-icon-pie-chart',
          path: '/layout/chart',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'user',
        component: User,
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          path: '/layout/user',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'question',
        component: Question,
        meta: {
          title: '题库列表',
          icon: 'el-icon-edit-outline',
          path: '/layout/question',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'enterprise',
        component: Enterprise,
        meta: {
          title: '企业列表',
          icon: 'el-icon-office-building',
          path: '/layout/enterprise',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'subject',
        component: Subject,
        meta: {
          title: '学科列表',
          icon: 'el-icon-notebook-2',
          path: '/layout/subject',
          roles: ['超级管理员', '管理员', '老师']
        }
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
/**
 * 执行：只要有路由跳转，它就会进来这里，在这里，我们就可以对它做权限判断
 * to：到哪里去
 * from：从何而来
 * next：决定能否过去，以及到哪里去
 */
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = getToken()
    if (token) {
      // 有权限
      next()
    } else {
      // 没有权限
      next('/login')
    }
  }
})

/**
 * 导航守卫之后置守卫
 * 当路由跳转完之后，再执行的函数
 */
router.afterEach((to, from) => {
  // ...
  document.title = to.meta.title || '黑马面面'
})
export default router
